package com.lee.carinforecord;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private static MainActivity activityInstance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        activityInstance = this;

        Button btnSendBroadcast = (Button) findViewById(R.id.btnSendBroadcast);
        Button btnHideWindow = (Button) findViewById(R.id.hideWindow);

        EditText edit = (EditText) findViewById(R.id.historyInfo);
        String his = FileTools.getHistory();
        edit.append(his);

        /*Integer mile = DataSave.getAvailableMile(MainActivity.this);
        Log.i("mile", mile + "");
        updateEditView(mile+"\n");
        String info = DataSave.getTrips(MainActivity.this);
        updateEditView(info+"\n");*/

        btnHideWindow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InfoWindow.hide();
            }
        });

        btnSendBroadcast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("canbus.intent.action.msg");
                byte[] bs = new byte[] { (byte)0x2e, (byte)0x33, (byte)0x0f, (byte)0x01, (byte)0x00, (byte)0x03, (byte)0x60, (byte)0x00, (byte)0x52, (byte)0x03, (byte)0x60, (byte)0x00, (byte)0x00, (byte)0x03, (byte)0x00, (byte)0x61, (byte)0x2a, (byte)0x03, (byte)0x12};
                //Log.i("dataLength", bs.length + "");
                intent.putExtra("canbus_data", bs);
                sendBroadcast(intent); // 发送广播
            }
        });
    }

    public static MainActivity getInstance() {
        return activityInstance;
    }

    public void updateEditView(final String t) {
        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
                EditText edit = (EditText) findViewById(R.id.carInfoReceive);
                edit.append(t);
            }
        });
    }
}